<template>
  <div class="app">
    <el-card class="box-card">
      <h1>我是登录组件</h1>
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        label-width="70px"
        class="demo-ruleForm"
      >
        <el-form-item label="用户名" prop="name">
          <el-input v-model="ruleForm.name"></el-input>
        </el-form-item>
        <el-form-item label="密 码" prop="pwd">
          <el-input v-model="ruleForm.pwd"></el-input>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')"
            >立即创建</el-button
          >
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import axios from "axios";

export default {
  name: "",
  data() {
    var validateName = (rule, value, callback) => {
      if (value.includes("admin")) {
        callback();
      } else {
        callback("用户名必须包含admin");
      }
    };

    return {
      ruleForm: {
        name: "",
        pwd: ""
      },
      rules: {
        name: [
          { required: true, message: "请输入名称", trigger: "blur" },
          {
            min: 3,
            max: 10,
            message: "长度在 3 到 10 个字符",
            trigger: "blur"
          },
          { validator: validateName, trigger: "blur" }
        ],
        pwd: [
          { required: true, message: "请输入名称", trigger: "blur" },
          {
            min: 5,
            max: 11,
            message: "长度在 5 到 11 个字符",
            trigger: "blur"
          }
          //   { pattern: /^1[3-9]\d{9}$/, message: "ok", trigger: "blur" }
        ]
      }
    };
  },
  watch: {},
  props: {},
  components: {},
  computed: {},
  mounted() {},
  methods: {
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },

    /*  submitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          this.$message({
            message: "成功!!!!!!芜湖",
            type: "success"
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    } */

    submitForm(formName) {
      this.$refs[formName]
        .validate()
        .then(() => {
          this.$message({
            message: "成功!!!!!!芜湖",
            type: "success"
          });

          axios
            .post("http://localhost:3000/user/login", {
              username: "admin",
              password: "123456"
            })
            .then(res => console.log(res));

          window.localStorage.setItem("isLogin", true);

          this.$router.push("/a");
        })
        .catch(() => {
          this.$message.error("失败失败失败~");
        });
    }
  },
  filters: {}
};
</script>

<style scoped>
.app {
  height: 100%;
}

.box-card {
  width: 500px;
  height: 500px;
  text-align: center;
  margin: 50px auto;
  background-color: #fff;
}
</style>
